File and directory information in C#

The file and directory classes that we used in the last few parts are great for file and directory manipulation directly. However, sometimes we want to get information about them, and once again, the System.IO namespace comes in our defense: FileInfo and DirectoryInfo classes In this section, we have some ways of using these two categories Will investigate.

First, let's explore a simple way to use the FileInfo class.


static void Main(string[] args)
{
    FileInfo fi = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
    if(fi != null)
        Console.WriteLine(String.Format("Information about file: {0}, {1} bytes, last modified on {2} - Full path: {3}", fi.Name, fi.Length, fi.LastWriteTime, fi.FullName));
    Console.ReadKey();
}

We create a new example of FileInfo Class. It takes a parameter, which is the path to the file that we want to know. We could only specify one filename, but for the joke, I thought it would be good to get information about the actual application on which we are working, that is, since our project was compiled XE file We do not have access to the application project in the console application (this is part of the WinForms assembly), so we have the ability to get the path to the current assembly The little use of Nb. It is outside the scope of this special section, but at least now you know

Once we give a file infographic example, we produce all kinds of information about it. Try running the application and you will see All are very nice and easy, and if you are looking at the file Infow, you will see that this file provides even more information for the methods found on the class, and why not? In the context of the file with the file example we have a reference, so we can get the same option as the file class.

Now, the information about a file is just fine, but using the DirectoryInfo class, we can get information about all the files and directories in a directory, which is clearly a very common scenario. I show you a simple example:


DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
if(di != null)
{
    FileInfo[] subFiles = di.GetFiles();
    if(subFiles.Length > 0)
    {
        Console.WriteLine("Files:");
        foreach(FileInfo subFile in subFiles)
        {
            Console.WriteLine("   " + subFile.Name + " (" + subFile.Length + " bytes)");
        }
    }
    Console.ReadKey();
}

Instead of a FileInfo example, we make a DirectoryInfo example. We use the same trick to obtain the path of the execution file, and then from the path class to get the directory from the GetDirectoryName () method, only the path to receive. We use the GetFiles () method to obtain an array of FileInfo instances, representing each file in each directory. We print loop, each filename and size through it.

Perhaps we want the directories as well. It's just as easy:


DirectoryInfo[] subDirs = di.GetDirectories();
if(subDirs.Length > 0)
{
    Console.WriteLine("Directories:");
    foreach(DirectoryInfo subDir in subDirs)
    {
        Console.WriteLine("   " + subDir.Name);
    }
}

In some circumstances, you want files or directories with only specific names or file extensions. Fortunately, FileInfo and DirectoryInfo also have some great support for that.

This will give us all files in the directory with a .exe extension:


FileInfo[] subFiles = di.GetFiles("*.exe");

This will give us all the directories which have the word "test" somewhere in the name:


DirectoryInfo[] subDirs = di.GetDirectories("*test*");

We can even find both files and directories recursively, which means that it will search in subdirectories of subdirectories of.... the originial directory:


FileInfo[] subFiles = di.GetFiles("*.exe", SearchOption.AllDirectories);

To only search the toplevel directory, the code would have to look like this:


FileInfo[] subFiles = di.GetFiles("*.exe", SearchOption.TopDirectoryOnly);